#!/system/bin/sh

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [ ! -d /data/pkg ]; then
    mkdir -p /data/pkg
fi

if [ ! -d /data/cache ]; then
    mkdir -p /data/cache
fi

if [ -e /lib/ld-musl-aarch64.so.1 ]; then
	cp -rP /lib/ld-musl-aarch64.so.1 $(realpath ./lib)/libc.so
fi

export PATH=$(realpath ./bin):${PATH}
export LD_LIBRARY_PATH=$(realpath ./lib):${LD_LIBRARY_PATH}
export PKGS_ROOT=/data/pkg
export CACHE_ROOT=/data/cache

ferrium_setup \
    --src-dir $(pwd) \
    --dst-dir /data/local/release \
    --extra-args $(realpath ./config)/ohos-cross/aarch64-pkgs/basic.toml \
    --extra-args $(realpath ./config)/ohos-cross/aarch64-pkgs/ros1.toml

# merge aarch64-linux-ohos
cp -rP /data/local/release/host/usr/lib/aarch64-linux-ohos/lib* /data/local/release/usr/lib/
rm -rf /data/local/release/host

chmod +x setup/scripts/*
cp -rP setup/scripts/* /system/bin/
